const cookieparser = process.server ? require('cookieparser') : undefined

//防止数据冲突
export const state = () => {
    return {
        user: null
    }
}

export const mutations = {

    setUser (state, data) {
        state.user = data
    },
    getUser(){
        return state.user
    },
    unsetUser(){
       state.user = null
    }

}

export const actions = {
    nuxtServerInit ( { commit }, {  req } ) {
        let user = null
        if (req.headers.cookie) {
            const parsed = cookieparser.parse(req.headers.cookie)
            console.log('server:'+parsed)
            console.dir(parsed)
            try{
                user = JSON.parse(parsed.user)
            }catch (err) {
                console.dir(err)
            }
            commit('setUser',user)
        }
    }
}